package com.cyys.modules.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.common.validate.Detail;
import com.cyys.common.validate.Edit;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;
import com.cyys.modules.generator.model.GeneratorField;
import com.cyys.modules.generator.service.GeneratorFieldService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/generator/field")
public class GeneratorFieldAction extends BaseAction {

	@Resource
	private GeneratorFieldService generatorFieldService;

	@GetMapping("/list")
	@RequiresPermissions("generator:table:menu")
	public String list(Model model,String tableId) {
		model.addAttribute("tableId",tableId);
		return "module/generator/field/generatorField-list";
	}

	@ResponseBody
	@PostMapping("/list/listInfoNoPage")
	@RequiresPermissions("generator:table:list")
	public String listInfo(GeneratorField generatorField) {
		QueryWrapper qw = new QueryWrapper(generatorField);
		qw.orderByAsc("ordinal_position");
		return JSONData.toLayerDataNoPage(generatorFieldService.listAll(qw));
	}


	@GetMapping("/edit/input")
	@RequiresPermissions("generator:table:edit")
	public String updateInput(@Validated(Detail.class) GeneratorField generatorField, Model model) {
		model.addAttribute("yesNoMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.YES_NO));
		model.addAttribute("inputTypeMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.GENERATOR_INPUT_TYPE));
		model.addAttribute("requiredMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.GENERATOR_REQUIRED));
		model.addAttribute("sysParamMap", ParamBasicUtil.getParamBasic());

		model.addAttribute("generatorField", generatorFieldService.getById(generatorField.getId()));
		return "module/generator/field/generatorField-input";
	}
	
	@ResponseBody
	@PostMapping("/edit/save")
	@RequiresPermissions("generator:table:edit")
	public AR edit(@Validated(Edit.class) GeneratorField generatorField) {
		return generatorFieldService.update(generatorField);
	}


	@ResponseBody
	@PostMapping("/list/listInfo")
	@RequiresPermissions("generator:table:list")
	public String listInfo(GeneratorField generatorField, Page page) {
		return JSONData.toLayerData(generatorFieldService.list(page , new QueryWrapper(generatorField),null));
	}


	@ResponseBody
	@PostMapping("/del/deleteById")
	@RequiresPermissions("generator:table:del")
	public AR deleteById(String id){
		return generatorFieldService.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/del/deleteByIds")
	@RequiresPermissions("generator:table:del")
	public AR deleteByIds(String ids) {
		return generatorFieldService.deleteBatchIds(ids);
	}
}
